home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / string / strtol.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  1KB  |  71 lines

  1.  
  2. /*
  3.  *  STRTOL.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #ifndef HYPER
  14. #define HYPER(x) x
  15. #endif
  16.  
  17. long
  18. HYPER(strtol)(ptr, tail, base)
  19. const char *ptr;
  20. char **tail;
  21. int base;
  22. {
  23.     long v = 0;
  24.     short ishex = 0;
  25.     short c;
  26.     short neg = 0;
  27.  
  28.     while (*ptr == ' ' || *ptr == '\t')
  29.     ++ptr;
  30.     if (*ptr == '-') {
  31.     neg = 1;
  32.     ++ptr;
  33.     } else if (*ptr == '+')
  34.     ++ptr;
  35.  
  36.     if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
  37.     ishex = 1;
  38.  
  39.     if (base == 0) {
  40.     base = 10;
  41.     if (ptr[0] == '0') {
  42.         base = 8;
  43.         if (ishex)
  44.         base = 16;
  45.     }
  46.     }
  47.     if (base == 16 && ishex)
  48.     ptr += 2;
  49.     for (;;) {
  50.     c = *ptr;
  51.     if (c >= '0' && c <= '9')
  52.         c -= '0';
  53.     else if (c >= 'a' && c <= 'z')
  54.         c -= ('a' - 10);
  55.     else if (c >= 'A' && c <= 'Z')
  56.         c -= ('A' - 10);
  57.     else
  58.         break;
  59.     if (c >= base)
  60.         break;
  61.     v = v * base + c;
  62.     ++ptr;
  63.     }
  64.     if (tail)
  65.     *tail = ptr;
  66.     if (neg)
  67.     v = -v;
  68.     return(v);
  69. }
  70.  
  71.